package xyz.ranyh.jobclient.util;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.util.Log;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import static android.content.Context.ALARM_SERVICE;

/**
 * Created by zhaoshengping on 2016/12/28.
 */

public class JobBean {
    private int id;
    private String title;
    private String url;
    private String refUrl;
    private int followerCount;
    private String openDate;
    private int openDateInt;
    private String publishedDate;
    private String[] tags;
    private Boolean isStar = null;
    private String alarm = null;

    public JobBean() {
    }

    public JobBean(String refUrl) {
        this.refUrl = refUrl;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public String getRefUrl() {
        return refUrl;
    }

    public void setRefUrl(String refUrl) {
        this.refUrl = refUrl;
    }

    public int getFollowerCount() {
        return followerCount;
    }

    public void setFollowerCount(int followerCount) {
        this.followerCount = followerCount;
    }

    public String getOpenDate() {
        return openDate;
    }

    public void setOpenDate(String openDate) {
        this.openDate = openDate;
    }

    public int getOpenDateInt() {
        return openDateInt;
    }

    public void setOpenDateInt(int openDateInt) {
        this.openDateInt = openDateInt;
    }

    public String getPublishedDate() {
        return publishedDate;
    }

    public void setPublishedDate(String publishedDate) {
        this.publishedDate = publishedDate;
    }

    public String[] getTags() {
        return tags;
    }

    public String getTagsString() {
        String str = null;
        int count = tags.length >= 5 ? 5 : tags.length;

        for (int i = 0; i < count; ++i) {
            if (str == null) {
                str = String.valueOf(tags[i]);
            } else {
                str += ", " + tags[i];
            }
        }
        return str;
    }

    public void setTags(String tags) {
        this.tags = tags.split(",");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public Boolean getStar() {
        return isStar;
    }

    public void setStar(Boolean star) {
        isStar = star;
    }

    public String getAlarm() {
        return alarm;
    }

    public void setAlarm(String alarm) {
        this.alarm = alarm;
        Log.d("alarm", alarm);
        if (!alarm.equals("null")) {
            startAlarmService();
        }
    }

    private boolean _isVT(String time) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd H:m");
        try {
            Date date = sdf.parse(time);
            return date.after(new Date());
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return false;
    }

    public boolean isValidTime(String time) {
        return _isVT(time);
    }

    public boolean isValidTime() {
        return _isVT(alarm);
    }

    private void startAlarmService() {
        Context context = Application.getContext();
        Date date = null;

        Intent intent = new Intent(context, OneAlarmReceiver.class);
        PendingIntent sender = PendingIntent.getBroadcast(context, 0, intent, 0);

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd H:m");
            date = sdf.parse(alarm);

            if (date.after(new Date())) {

                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                calendar.set(Calendar.SECOND, 0);

                // Schedule the alarm!
                AlarmManager am = (AlarmManager) context.getSystemService(ALARM_SERVICE);
                am.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), sender);
            }
        }
        catch (ParseException e) {
            Log.d("Date format exception", e.getMessage());
        }
    }
}
